﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "GmStructure.h"
#include "Vector3d.h"
#include "ElementJoinUtils.h"
#include "JoinPositions.h"
#include "IElementJoin.h"

namespace gcmp
{
   class IElement;
   class IInstance;
   class IDocument;
   class IElementJoin;

   using IsAllowJoinFunction = bool(*)(const IElement* pElem);

   struct GM_STRUCTURE_EXPORT GbmpJoinUtils
   {
      //创建连接和扣减关系
      //1、若pInstance为墙，则创建墙墙连接和墙柱扣减关系
      //2、若pInstance为柱，则创建墙柱扣减关系
      //3、若pInstance为梁，则创建梁梁连接关系
      static bool CreateJoinsAndCuts(gcmp::IDocument* pDoc, gcmp::IInstance* pInstance);
      static bool CreateJoinsAndCuts(gcmp::IDocument* pDoc, const gcmp::ElementId& id);

      //单个对象间在端点与端点建立连接关系
      static IElementJoin* CreateLTypeJoin(gcmp::IDocument* pDoc, const gcmp::ElementId& attachingId, const gcmp::ElementId& attachedId);
      static IElementJoin* CreateLTypeJoin(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, gcmp::IInstance* pAttachedInstance);

      static IElementJoin* CreateLTypeJoinByJoinPosition(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, gcmp::JoinPosition attachingJoinPosition, gcmp::IInstance* pAttachedInstance, gcmp::JoinPosition attachedJoinPosition);

      // targetInstances - 目标几何，如果targetInstances有值，返回值必须在targetInstances中。
      static std::vector<gcmp::IInstance*> GetAttachedInstances(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, IsAllowJoinFunction isAllowJoinFunc, const std::set<ElementId>& targetInstances = std::set<ElementId>());

      //通过八叉树找墙，然后进行判断是否允许建立墙墙连接
      //(1) 允许端点误差范围内，尝试创建主动的端接(若端线相交，则会创建连接)
      //(2) 如果端部未连接上，尝试创建主动的T接(注意区分从动T接)
      //(3) tryPassiveJoin - 主动连接失败后，是否尝试被动连接
      //创建墙墙连接点
      static bool CreateWallWallJoins(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, bool tryPassiveJoin = true);
      //创建梁梁连接点
      static bool CreateBeamBeamJoins(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingBeam, bool tryPossiveJoin = true);

      static bool AdjustControlPointToInstance(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, gcmp::JoinPosition attachingJoinEnd, const std::vector<gcmp::IInstance*>& pAttachedInstances);
      static bool AdjustBeamControlPointsToInstance(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, IsAllowJoinFunction isAllowJoinFunc, const std::set<ElementId>& targetInsts = std::set<gcmp::ElementId>());
      static bool AdjustControlPointOrCreateJoin(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, gcmp::JoinPosition attachingJoinEnd, const std::vector<gcmp::IInstance*>& pAttachedInstances);
      static bool AdjustWallControlPointsOrCreateJoins(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, const std::set<ElementId>& targetInsts = std::set<gcmp::ElementId>());
      static bool AdjustBeamControlPointsOrCreateJoins(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, const std::set<ElementId>& targetInsts = std::set<gcmp::ElementId>());


      //创建条形基础连接点
      static bool CreateStripFoundationJoins(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance);

      static void CheckAndTryToReverseRelationshipOrDeleteJoin(gcmp::IDocument* pDoc, IElementJoin* pJoin);

      static IElementJoin* CreateLTypeJionBySectionalAreaPriority(gcmp::IDocument* pDoc, IInstance* pAttachingInstance, JoinPosition attachingPos, IInstance* pAttachedInstance, JoinPosition attachedPos);
      static IElementJoin* CreateLTypeJion(gcmp::IDocument* pDoc, IInstance* pAttachingInstance, JoinPosition attachingPos, IInstance* pAttachedInstance, JoinPosition attachedPos);

      static bool  GetJoinData(const IInstance* pInstance, JoinPosition position, const IInstance* pOtherInstance, JoinPosition otherPosition, JoinType type, JoinData& relationship);


      static std::pair<int, int> GetInstanceJoinPositionIndex(const gcmp::IInstance* pAttachingInstance, const gcmp::IInstance* pAttachedInstance);

      //找距ptPoint最近的端点
      static gcmp::JoinPosition GetClosestEndPointJoinPosition(gcmp::IInstance* pInstance, const gcmp::Vector3d& ptPoint);

   private:
      static bool GetPriAndSecRelationship(const IInstance* pInstance, const IInstance* pOtherInstance, JoinType type, std::pair<ElementId, ElementId>& relationship);
      static bool DoLTypeJoin(gcmp::IDocument* pDoc, gcmp::IInstance* pAttachingInstance, gcmp::JoinPosition attachingJoinEnd, IInstance* pAttachedInstance, bool isRepairModel = false);
      static bool CreateJoinsByJoinPosition(gcmp::IInstance* pAttachingInstance, gcmp::JoinPosition attachingJoinPosition, const std::vector<gcmp::IInstance*>& pAttachedInstances);

      /// \brief    创建T形连接
      /// \param pAttachingInstance 次对象（改变）
      /// \param pAttachedInstance  主对象（不动）
      /// \return 返回true表示创建成功，否则表示失败。
      static bool CreateTTypeJoins(IInstance* pAttachingInstance, IInstance* pAttachedInstance);

   };
}

